package cn.cjy.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import cn.cjy.dao.UserinfoDao;

import cn.cjy.model.Userinfo;
import cn.cjy.utils.DBUtil;
import cn.cjy.utils.MyUtil;
import cn.cjy.utils.MysqlUtil;
import cn.cjy.utils.ResultHandle;

public class UserinfoDaoImpl implements UserinfoDao {

	Connection conn = null;
	Statement stmt = null;
	ResultSet rs = null;
	List<Userinfo> uo = null;
	int n = 0;
	@Override
	public List<Userinfo> all() {
		List<Userinfo> userinfos = new ArrayList<Userinfo>();
		try {
			String sql = "select * from userinfo where 1=1";
			ResultSet rs = MysqlUtil.excuteMysqlSql(sql);
			Userinfo userinfo = null;
			while (rs.next()) {
				userinfo = new Userinfo();
				userinfo.setUserinfoid(rs.getInt("userinfoid"));
				userinfo.setEmail(rs.getString("email"));
				userinfo.setPassword(rs.getString("password"));
				userinfos.add(userinfo);
			}
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return userinfos;
	}

	@Override
	public Userinfo one(String userinfoid) {
		String sql = "select * from userinfo where userinfoid=" + userinfoid;
		ResultSet rs = MysqlUtil.excuteMysqlSql(sql);
		Userinfo userinfo = null;
		try {
			if (rs.next()) {
				userinfo = new Userinfo();
				userinfo.setUserinfoid(rs.getInt("userinfoid"));
				userinfo.setEmail(rs.getString("email"));
				userinfo.setPassword(rs.getString("password"));
			}
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return userinfo;
	}

	@Override
	public Integer update(Userinfo userinfo) {
		try {
			conn = DBUtil.getConnection();
			String sql = "update userinfo set email=?,password=? where email=?";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(3,userinfo.getEmail());
			pstmt.setString(1,userinfo.getEmail());	
			pstmt.setString(2,userinfo.getPassword());		

			n = pstmt.executeUpdate();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("n=" + n);
		return n;
	}

	@Override
	public Integer add(Userinfo userinfo) {
		// TODO Auto-generated method stub
		String sql = " insert into userinfo (email,password) values ('" + userinfo.getEmail() + "','"
				+ userinfo.getPassword() + "')";

		Integer row = MysqlUtil.excuteMysqlUpdate(sql);

		return row;
	}

	@Override
	public Integer delete(String userinfoid) {
		// TODO Auto-generated method stub
		String sql = "delete from userinfo where userinfoid=" + userinfoid;

		Integer row = MysqlUtil.excuteMysqlUpdate(sql);

		return row;
	}

	@Override
	public Userinfo login(String email, String password) {
		String sql="select * from userinfo where email=? and password=?";
		Userinfo userinfo=MyUtil.query(sql, new ResultHandle<Userinfo>() {

			@Override
			public Userinfo handle(ResultSet rs) {
				// TODO Auto-generated method stub
				Userinfo userinfo=new Userinfo();
				try {
					while(rs.next()) {
						userinfo.setUserinfoid(rs.getInt("userinfoid"));
						userinfo.setEmail(rs.getString("email"));
						userinfo.setPassword(rs.getString("password"));
					}
					return userinfo;
				}catch (SQLException e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				return null;
			}
			
		}, new Object[] {email,password});
		return userinfo;
	}

	@Override
	public List<Userinfo> selectUserByEmail(String key) {
		try {
			conn = DBUtil.getConnection();
			String sql = "select * from userinfo where email like ?";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,"%".concat(key).concat("%"));
			rs = pstmt.executeQuery();
			uo=new ArrayList<Userinfo>();
			while(rs.next()) {
				Userinfo userinfo=new Userinfo();
				userinfo.setUserinfoid(rs.getInt("userinfoid"));
				userinfo.setEmail(rs.getString("email"));
				userinfo.setPassword(rs.getString("password"));;
				uo.add(userinfo);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return uo;
	}
}
